<template>
  <div class="container">

    <h1>我的收藏</h1>
    
    <!-- <div class="pagination-container">
      <div class="pagination">
        <button 
          @click="prevPage" 
          :disabled="page.pageNo === 1"
          class="page-btn prev-btn">
          上一页
        </button>
        
        <button 
          @click="nextPage" 
          :disabled="page.pageNo === totalPage"
          class="page-btn next-btn">
          下一页
        </button>
      </div>
      <div class="page-info">
        共 {{ total }} 条， {{ totalPage }} 页，当前为第 {{ page.pageNo }} 页 ，该页 {{ articleList.length }} 条
      </div>
    </div> -->

    <div class="article_list">
      <div v-for="article in articleList" class="article_card" :key="article.id" >
        <div>
          <div class="thumbnail">
            <img :src="article.picture" @click="handleArticle(article.id)"/>
          </div>
          <div class="info">
            <div class="title">{{ article.title }}</div>
            <span>👁 {{ article.views }}</span>
            <button @click="handleUnCollect(article.id)">取消收藏</button>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>
  
<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getUserCollectArt } from '@/api/BlogUser'
import { unCollectArt } from '@/api/BlogUser'
import hahaPng from '@/assets/1234.jpg'
import server from '@/api/server'

const router = useRouter();

const articleList = ref([{
  id: 1,
  userId: 1,
  picture: hahaPng,
  title: 'hahahaha',
  views: 999
}])

const page = ref({
  pageNo: 1,
  pageSize: 10
});


onMounted(()=>{
  if(server.token == ''){
    router.push("/notLogin")
    return
  }
  getData()
})

const getData = () =>{
  try {
    getUserCollectArt(server.token, page.value).then((res)=>{
      if(res.data.code == 0){
        console.log(res.data.data);
        articleList.value = res.data.data;
        articleList.value = res.data.data;
      }else{

        alert("加载失败："+res.data.msg);

      }
    })
  } catch (error) {

    alert("加载失败："+error);
    
  }
}

const handleArticle = (articleId)=>{
  server.currentPageId = articleId;
  router.push("/detail");
}

const handleUnCollect = (articleId)=>{
  const params = {
    'articleId': articleId
  }
  try {
    unCollectArt(server.token, params).then((res)=>{
      alert(res.data.data)
      getData()
    })
  } catch (error) {
    
    alert("取消关注失败：" + error);

  }
}



// const total = ref(0);

// const totalPage = ref(0)

// const prevPage = () => {
//   // page.value.pageNo -= 1
//   // getPage()
// };

// const nextPage = () => {
//   // page.value.pageNo += 1
//   // getPage()
// };
</script>
  
<style scoped>
.container{
  width: 800px;
  margin: 60px auto;
}

h1{
  margin-bottom: 60px;
}

/* 文章列表 */
.article_list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin: 20px 0vw 120px;
}
/* 文章卡片 */
.article_card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 0 6px rgba(0,0,0,0.05);
  transition: transform 0.2s;
}

.article_card:hover {
  transform: translateY(-4px);
}

/* 缩略图 */
.thumbnail {
  height: 180px;
  overflow: hidden;
  position: relative;
}

.thumbnail img {
  height: 100%;width: 100%;
  object-fit: cover;
}

/* 文章信息 */
.info {
  padding: 8px 10px;
}

.info .title {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.info button{
  display: block;  margin: 0 auto;
  width: 70px;height: 30px;

  background-color: #ff6699;
  border: none;
  color: white;
  border-radius: 4px;
}



/* 分页样式 */
.pagination-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 30px 0;
  width: 100%;
}

.pagination {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 15px;
}

.page-btn {
  padding: 8px 16px;
  border: 1px solid #dcdfe6;
  background: #ffffff;
  color: #606266;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 14px;
  font-weight: 500;
}

.page-btn:hover:not(:disabled) {
  background: #409EFF;
  color: #ffffff;
  border-color: #409EFF;
}

.page-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  color: #909399;
  font-size: 14px;
  text-align: center;
  line-height: 1.6;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .article_item {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .item-content {
    width: 100%;
    margin-bottom: 12px;
  }
  
  .edit-btn, .del-btn {
    align-self: flex-end;
    margin-left: 8px;
    margin-top: 10px;
  }
  
  .pagination {
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .page-info {
    font-size: 13px;
  }
}
</style>
  